﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using DevExpress.LocalizationCHS;
using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;

using DevExpress.XtraEditors.Repository;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;


namespace JSales.ModuleProduct
{
    public partial class TextureSetForm : Form
    {
        public TextureSetForm()
        {
            InitializeComponent();
        }
        
        private void CtrolINI()
        {
            barButtonAdd.Enabled = true;
            barButtonSave.Enabled = false;

            gridView1.OptionsBehavior.Editable = false;
            gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;
            gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
        }



        private void TextureSetForm_Load(object sender, EventArgs e)
        {
            //汉化界面
       //     DevExpress.XtraGrid.Localization.GridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraGridLocalizationCHS();
       //     DevExpress.XtraTreeList.Localization.TreeListLocalizer.Active= new DevExpressXtraTreeListLocalizationCHS();
            
            gridControl1.DataSource = GlobalVar.GTextures.ToList();
            //一定要转换为BindingList<>对象绑定，才能触发GridView的InitNewRow事件。

            gridView1.Columns["TEXTUREID"].FieldName = "TEXTUREID";
            gridView1.Columns["TEXTUREID"].Caption = "纹路ID";
            gridView1.Columns["TEXTUREID"].VisibleIndex = 0;

            gridView1.Columns["TEXTURENAME"].FieldName = "TEXTURENAME";
            gridView1.Columns["TEXTURENAME"].Caption = "纹路";
            gridView1.Columns["TEXTURENAME"].VisibleIndex = 1;

            gridView1.Columns["ISDELETED"].FieldName = "ISDELETED";
            gridView1.Columns["ISDELETED"].Caption = "停用标志";
            gridView1.Columns["ISDELETED"].VisibleIndex = 2;
            
            CtrolINI();
        }
        
        private void GridView1InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            view.SetRowCellValue(e.RowHandle, view.Columns["TEXTUREID"], "");
            view.SetRowCellValue(e.RowHandle, view.Columns["TEXTURENAME"], "");
            this.gridView1.UpdateCurrentRow();
            //var user = new TbTexture();
        }
        
        private void BarButtonAddItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.OptionsBehavior.Editable = true;
            gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True;
            gridView1.OptionsView.NewItemRowPosition=NewItemRowPosition.Top;
            barButtonSave.Enabled = true;
        }

        private void barButtonSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if(GlobalVar.GTextures.Any())
            {
//                foreach (DataRow row in GlobalVar.GlobaDs.Tables["Texture"].Rows)
//                {
//                    switch (row.RowState)
//                    {
//                        case DataRowState.Added:
//                            TbTexture Texture_a = new TbTexture();
//                            Texture_a.Textureid = row["TEXTUREID"].ToString();
//                            Texture_a.Texturename = row["TEXTURENAME"].ToString();
//                            Texture_a.Isdeleted = false;
//                            TextureClass.Create(Texture_a);
//                            this.TextureSetForm_Load(sender, e);
//                            break;
//                        case DataRowState.Deleted:
//                            TbTexture Texture_d = TextureClass.Query(row["TEXTUREID"].ToString());
//                            Texture_d.Isdeleted = true;
//                            TextureClass.Update(Texture_d);
//                            this.TextureSetForm_Load(sender, e);
//                            break;
//                        case DataRowState.Modified:
//                            TbTexture Texture_m = TextureClass.Query(row["TEXTUREID"].ToString());
//                            Texture_m.Textureid = row["TEXTUREID"].ToString();
//                            Texture_m.Texturename = row["TEXTURENAME"].ToString();
//                            Texture_m.Isdeleted = bool.Parse(row["ISDELETED"].ToString());
//                            TextureClass.Update(Texture_m);
//                            this.TextureSetForm_Load(sender, e);
//                            break;
//                        case DataRowState.Unchanged:
//                            //MessageBox.Show("Unchanged");
//                            break;
//                    }
//                } 
//                GlobalVar.GlobaDs.Tables["Texture"].AcceptChanges();
            }

        }

        private void barButtonEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.OptionsBehavior.Editable = true;
            gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            barButtonSave.Enabled = true;
        }

        private void barButtonCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl1.FocusedView.CloseEditor();
            CtrolINI();
        }
        
        private void gridView1_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            GridView view = sender as GridView;
            GridColumn IDColumn = view.Columns["TEXTUREID"];
            if (view.GetRowCellValue(e.RowHandle, IDColumn).Equals("") || view.GetRowCellValue(e.RowHandle, IDColumn).Equals(null))
            {
                e.Valid = false;
                view.SetColumnError(IDColumn, "纹路ID不能为空！");
            }
            if (view.GetRowCellValue(e.RowHandle, IDColumn).ToString().Length>3)
            {
                e.Valid = false;
                view.SetColumnError(IDColumn, "纹路ID长度不能大于3！");
            }   
            



            //else
            //{
            //    if (view.GetRowCellValue(e.RowHandle, IDColumn).ToString().Length > 3)
            //    {
            //        e.Valid = false;
            //        view.SetColumnError(IDColumn, "ID长度超过3位");
            //    }
            //}
            
            
            /*
            DataRowView focusedRow = (DataRowView)gridView1.GetFocusedRow();
            if (!focusedRow.IsNew) return;
            foreach (DataColumn column in focusedRow.DataView.Table.Columns)
            {
                e.Valid = !focusedRow.Row.IsNull(column);
                if (!e.Valid) return;
            }
            */

        }

        private void gridView1_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
            //另一种异常模式
            //e.ExceptionMode = ExceptionMode.DisplayError;
            //e.WindowCaption = "输入错误！";
        }

        private void gridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {

        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }
    }
}
